package com.house365.core.view.text;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.widget.TextView;

/**
 *@author py
 *@date 2012-12-17
 *@comment user control,可展开收缩的textview,只需要配置基本属性以及lines属性（收缩情况最多多少行）
 */
public class ExpandableTextView extends TextView {
	private int maxLines;
	int lines ;
	int currentLine;
	private int extend_height = 5;
	public static final int STATE_HIDE = 0;
	public static final int STATE_SHOW = 1;
	int state = STATE_HIDE;
	
	public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		getLines(attrs);
	
	}
	private int getLines(AttributeSet attrs){
		String s = attrs.getAttributeValue(  
                "http://schemas.android.com/apk/res/android","lines");  
		try {
			lines = Integer.parseInt(s);
		} catch (NumberFormatException e) {
			lines = -1;
		}
		return lines;
	}
	public ExpandableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		getLines(attrs);
	}

	public ExpandableTextView(Context context) {
		super(context);
	}
	/**
	 * the three override method be used to resolve a bug of Jelly Bean, use textview.setText(html.fromHtml(s)) occurs ArrayIndexOutBoundsException
	 * reference:https://code.google.com/p/android/issues/detail?id=35466
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		try{
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}catch(ArrayIndexOutOfBoundsException e){
			setText(getText().toString());
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}
	@Override
	public void setGravity(int gravity) {
		try{
			super.setGravity(gravity);
		}catch(ArrayIndexOutOfBoundsException e){
			setText(getText().toString());
			super.setGravity(gravity);
		}
	}
	@Override
	public void setText(CharSequence text, BufferType type) {
		try{
			super.setText(text, type);
		}catch(ArrayIndexOutOfBoundsException e){
			setText(text.toString());
		}
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		if(maxLines <=0){
			maxLines = getLineCount();
			boolean has = false;
			if(maxLines>lines){
				has = true;
				setEllipsize(TruncateAt.END);
				setLines(lines);
				
			}
			getLayoutParams().height = lines*getLineHeight()+extend_height;
			if(textLineListener!= null){
				textLineListener.hasMore(has);
			}
			
		}
	
		//通知外部变化
	}
	public void show(int duration){
		setLines(maxLines);
		state  = STATE_SHOW;
		int finishHeight = maxLines*getLineHeight()+extend_height;
		int startHeight = lines*getLineHeight()+extend_height;
		ExpandAnimation anim = new ExpandAnimation(this, startHeight, finishHeight);
		anim.setDuration(duration);
		this.startAnimation(anim);
	}
	public void hide(int duration){
		state  = STATE_HIDE;
		int finishHeight = lines*getLineHeight()+extend_height;
		int startHeight = maxLines*getLineHeight()+extend_height;
		ExpandAnimation anim = new ExpandAnimation(this, startHeight, finishHeight);
		anim.setDuration(duration);
		anim.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				setLines(lines);
				setEllipsize(TruncateAt.END);
				
			}
		});
		this.startAnimation(anim);
		
//		setLines(lines);
	}
	private TextLineListener textLineListener ;
	
	public TextLineListener getTextLineListener() {
		return textLineListener;
	}
	public void setTextLineListener(TextLineListener textLineListener) {
		this.textLineListener = textLineListener;
	}

	public interface TextLineListener{
		public void hasMore(boolean has);
	}
	 class ExpandAnimation extends Animation {
	        private final TextView _view;
	        private final int _startHeight;
	        private final int _finishHeight;

	        public ExpandAnimation( TextView view, int startHeight, int finishHeight ) {
	            _view = view;
	            _startHeight = startHeight;
	            _finishHeight = finishHeight;
	        }

	        @Override
	        protected void applyTransformation( float interpolatedTime, Transformation t ) {
	            final int newHeight = (int)((_finishHeight - _startHeight) * interpolatedTime + _startHeight);
	            _view.getLayoutParams().height = newHeight;
	            _view.requestLayout();
	        }

	        @Override
	        public void initialize( int width, int height, int parentWidth, int parentHeight ) {
	            super.initialize(width, height, parentWidth, parentHeight);
	        }

	        @Override
	        public boolean willChangeBounds( ) {
	            return true;
	        }
	    };
}
